package com.examsystem.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.examsystem.common.ResultJson;
import com.examsystem.common.StatusAndMsg;
import com.examsystem.dao.exame_paperMapper;
import com.examsystem.entity.exame_paper;
import com.examsystem.entity.exame_paperExample;
import com.examsystem.entity.exame_paperExample.Criteria;
import com.examsystem.service.PaperService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class PaperServiceImpl implements PaperService{

	@Autowired
	private exame_paperMapper paperMapper;

	//增加试卷
	public ResultJson insertPaper(exame_paper paper) {
		ResultJson result=null;
		//补全信息
		paper.setCreatetime(new Date());
		paper.setUpdatetime(new Date());
		//操作数据库
		int i = paperMapper.insertSelective(paper);
		if(i>0) {
			result=new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), null);
		}else {
			result=new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), StatusAndMsg.SERVICEERROR.getMsg(), null);
		}
		return result;
	}
	
	//修改试卷
	public ResultJson updatePaperByKey(exame_paper paper) {
		ResultJson result=null;
		//判断
		if(null==paper.getPid()) {
			result=new ResultJson(StatusAndMsg.PARAMEERROR.getStatus(), StatusAndMsg.PARAMEERROR.getMsg(), null);
			return result;
		}	
		//操作数据库
		int i = paperMapper.updateByPrimaryKeySelective(paper);
		if(i>0) {
			result=new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), null);
		}else {
			result=new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), StatusAndMsg.SERVICEERROR.getMsg(), null);
		}
		return result;
	}
	
	//删除试卷
	public ResultJson deletePaperByKey(exame_paper paper) {
		ResultJson result=null;
		//判断
		if(null==paper.getPid()) {
			result=new ResultJson(StatusAndMsg.PARAMEERROR.getStatus(), StatusAndMsg.PARAMEERROR.getMsg(), null);
			return result;
		}	
		//操作数据库
		int i = paperMapper.deleteByPrimaryKey(paper.getPid());
		if(i>0) {
			result=new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), null);
		}else {
			result=new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), StatusAndMsg.SERVICEERROR.getMsg(), null);
		}
		return result;
	}
	
	//查询试卷（模糊查询）试卷ID，名称，科目，出题人，
	public ResultJson selectPaperByExample(exame_paper paper,Integer page,Integer pageSize) {
		ResultJson result=null;
		//构造查询条件
		exame_paperExample example=new exame_paperExample();
		Criteria criteria = example.createCriteria();
		if(null!=paper.getPid()) {
			//主键查询，唯一的
			criteria.andPidEqualTo(paper.getPid());
		}else {
			//试卷名称
			if(StringUtils.isNotBlank(paper.getPname())) {
				criteria.andPnameLike("%"+paper.getPname()+"%");
			}
			//科目id（前台id与名称对应）
			if(null!=paper.getStid()) {
				criteria.andStidEqualTo(paper.getStid());
			}
			//出题人
			if(StringUtils.isNotBlank(paper.getUsername())) {
				criteria.andUsernameLike("%"+paper.getUsername()+"%");
			}
		}
		//操作数据库
		/*PageHelper.startPage(page, pageSize);
		List<PapersResult> list = paperMapper.selectByExample(example);
		if(list.size()>0&&null!=list) {
			PageInfo<PapersResult> pageInfo=new PageInfo<>(list, pageSize);
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), pageInfo);
		}else {
			// 为空
			result = new ResultJson(StatusAndMsg.NODATA.getStatus(), StatusAndMsg.NODATA.getMsg(), null);
		}*/
		return result;
	}
	
}
